package com.qinsoft.frame.utils;

import java.io.PrintWriter;
import java.text.ParseException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.directwebremoting.WebContext;

import org.directwebremoting.WebContextFactory;


import com.google.gson.Gson;
import com.qinsoft.frame.common.UserInfo;
import com.qinsoft.frame.common.UserUtil;

/**
 * http工具类
 */
public abstract class BaseHttpUtils {

	protected static final String ENCODING_UTF8 = "UTF-8";
	protected static final String ENCODING_GBK = "GBK";
	protected static final String ENCODING_ISO = "ISO-8859-1";
	protected Logger log = Logger.getLogger(this.getClass());
	protected WebContext wc = WebContextFactory.get();

	/**
	 * 向页面输出字符
	 * 
	 * @param output
	 * @return
	 */
	protected void out(String output, String encoding) {
		PrintWriter out = null;
		try {
			HttpServletResponse response = this.getResponse();
			// 设置字符集
			response.setCharacterEncoding(encoding);
			out = response.getWriter();
			// 直接输入响应的内容
			out.write(output);
			out.flush();
		} catch (Exception e) {
			log.error("向页面输出数据出错", e);
		} finally {
			if (out != null)
				out.close();
		}
	}

	/**
	 * 输出访问状态码
	 * 
	 * @param code
	 */
	protected void setStatus(int code) {
		this.getResponse().setStatus(code);
	}

	/**
	 * 获取页面提交参数.
	 * 
	 * @param key
	 * @return
	 */
	protected String get(String key) {
		return this.getRequest().getParameter(key);
	}

	/**
	 * 获取页面提交参数.
	 * 
	 * @param key
	 * @return
	 */
	protected Object get(String key, ParameterType type) {
		String value = this.getRequest().getParameter(key);
		if (value == null)
			return null;
		if (type == ParameterType.LONG) {
			return Long.parseLong(value.trim());
		} else if (type == ParameterType.FLOAT) {
			return Float.parseFloat(value.trim());
		} else if (type == ParameterType.DOUBLE) {
			return Double.parseDouble(value.trim());
		} else if (type == ParameterType.INTEGER) {
			return Integer.parseInt(value.trim());
		} else if (type == ParameterType.DATE) {
			try {
				return DateUtils.parse(value.trim(), "yyyy-MM-dd");
			} catch (ParseException e) {
			}
		} else if (type == ParameterType.TIME) {
			try {
				return DateUtils.getDateByString(value.trim());
			} catch (ParseException e) {
				e.printStackTrace();
			}
		} else if (type == ParameterType.BOOLEAN) {
			return Boolean.parseBoolean(value.trim());
		}
		return value;
	}

	/**
	 * 向页面输出字符. 默认UTF-8字符集
	 * 
	 * @param output
	 * @return
	 */
	protected void out(String output) {
		out(output, ENCODING_UTF8);
	}

	/**
	 * 将对象以Json方式输出到页面上
	 * 
	 * @param src
	 */
	protected void outJson(Object src) {
		this.out((new Gson()).toJson(src));
	}

	/**
	 * 以Script标签包含输出字符
	 * 
	 * @param script
	 */
	protected void outScript(String script) {
		this.outScript(script, "UTF-8");
	}

	protected void outScript(String script, String encoding) {
		String outStr = "<HTML>\n<HEAD>\n<script type=\"text/javascript\">\n"
				+ script + "\n" + "</script>\n</HEAD>\n</HTML>";
		this.out(outStr, encoding);
	}

	/**
	 * 获取登陆用户.
	 * 
	 * @return User
	 */
	protected UserInfo getLoginUser() {
		return UserUtil.getInstance().getUserInfo(this.getSessiont().getId());
	}

	/**
	 * 添加Request属性
	 * 
	 * @param key
	 * @param obj
	 */
	protected void put(String key, Object obj) {
		getRequest().setAttribute(key, obj);
	}

	/**
	 * 获取HttpServletRequest
	 * 
	 * @return
	 */
	protected HttpServletRequest getRequest() {
		return wc.getHttpServletRequest();
	}

	/**
	 * 获取HttpSession
	 * 
	 * @return
	 */
	protected HttpSession getSessiont() {
		return wc.getSession();
	}

	/**
	 * 获取HttpResponse
	 * 
	 * @return HttpServletResponse
	 */
	public HttpServletResponse getResponse() {
		return wc.getHttpServletResponse();
	}

	/**
	 * 参数类型.
	 * 
	 * @author XinWang
	 * 
	 */
	protected enum ParameterType {
		INTEGER, LONG, STRING, DATE, FLOAT, TIME, DOUBLE, BOOLEAN
	}
}
